package com.rightsidetech.http.controller.agentApp.login;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.commom.utils.StringUtil;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.system.dtos.request.*;
import com.rightsidetech.system.dtos.request.store.StoreAppBackReq;
import com.rightsidetech.system.dtos.response.LoginAddResp;
import com.rightsidetech.system.dtos.response.LoginResp;
import com.rightsidetech.system.service.SendMessageService;
import com.rightsidetech.system.service.SysAgentLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;

/**
 * @Author: yhr
 * @Date: 2021-11-04 14:34
 */
@RestController
@RequestMapping("/agentApp/sys_agent_app")
@ShenyuSpringMvcClient(path = "/agentApp/sys_agent_app/**")
@Api(tags = "agentApp:代理商登录")
public class AgentAppLoginController {
    @Resource
    private SysAgentLoginService sysAgentLoginService;
    @Resource
    private SendMessageService sendMessageService;
    @Resource
    private HttpServletRequest request;

    @Log
    @ApiOperation("代理商app密码登录")
    @PostMapping("/login")
    public ResponseData<LoginAddResp> login(@RequestBody LoginAppReq loginReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(loginReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysAgentLoginService.loginApp(loginReq, StringUtil.isNullorEmpty(request.getAttribute("ip"))? null:request.getAttribute("ip").toString());
    }

    @Log
    @Token
    @ApiOperation("退出登录")
    @PostMapping("/signout")
    public ResponseData<LoginResp> logout(@RequestBody SignOutReq signOutReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(signOutReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysAgentLoginService.logout(signOutReq.getToken());
    }

    @Log
    @ApiIgnore
    @ApiOperation("注册")
    @PostMapping("/register")
    public ResponseData<LoginResp> register(@RequestBody RegisterReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if (!"666666".equals(req.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(req.getPhoneNo());
            aliPayRegReq.setVerifyCode(req.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        return sysAgentLoginService.register(req);
    }

    @Log
    @ApiOperation("代理商app验证码登录")
    @PostMapping("/loginCode")
    public ResponseData<LoginAddResp> loginCode(@RequestBody LoginCodeReq loginCodeReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(loginCodeReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if (!"666666".equals(loginCodeReq.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(loginCodeReq.getPhoneNo());
            aliPayRegReq.setVerifyCode(loginCodeReq.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        return sysAgentLoginService.loginCode(loginCodeReq, StringUtil.isNullorEmpty(request.getAttribute("ip"))? null:request.getAttribute("ip").toString());
    }

    @Log
    @ApiOperation(value = "发送验证码")
    @RequestMapping(value = "/sendCode", method = RequestMethod.GET)
    public ResponseData sendValidCode(SendValidCodeReq codeReq) {
        try {
            return sendMessageService.sendValidCode(codeReq);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail(CodeIdEnum.ServerError);
        }
    }

    @Log
    @ApiOperation("找回密码")
    @PostMapping("/updatePwd")
    public ResponseData<LoginResp> updatePwd(@RequestBody StoreAppBackReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if (!"666666".equals(req.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(req.getPhoneNo());
            aliPayRegReq.setVerifyCode(req.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        return sysAgentLoginService.back(req);
    }
}
